package
{
	import org.flixel.*;
	
	/**
	 * ...
	 * @author JAC
	 */
	public class Controller extends FlxObject
	{
		private static var _nextDirection:uint;
		private static var _lastDirection:uint;
		private static var _player:Player;
		
		public static function init():void
		{
			_nextDirection = NONE;
			_lastDirection = NONE;
			_player = Player.player;
			_player.stopMoving();
		}
		
		public static function advance():void
		{
			if (_nextDirection == NONE)
			{
				_nextDirection = _lastDirection;
				_lastDirection = NONE;
			}
		}
		
		public static function update():void
		{
			_player.stopAtExit(atExitCallback);
			checkInputs();
			moveBaseOnInput();
		}
		
		private static function checkInputs():void
		{
			if (FlxG.keys.justPressed("UP"))
			{
				_nextDirection = UP;
			}
			if (FlxG.keys.justPressed("DOWN"))
			{
				_nextDirection = DOWN;
			}
			if (FlxG.keys.justPressed("LEFT"))
			{
				_nextDirection = LEFT;
			}
			if (FlxG.keys.justPressed("RIGHT"))
			{
				_nextDirection = RIGHT;
			}
			if (_lastDirection != NONE && _nextDirection == _lastDirection)
			{
				_nextDirection = NONE;
			}
		}
		
		private static function atExitCallback():void
		{
			FlxG.shake(0.0025, 0.1);
		}
		
		private static function moveBaseOnInput():void
		{
			switch (_nextDirection)
			{
				case NONE: 
				{
					return;
				}
				case UP: 
				{
					if ((_lastDirection == NONE || _lastDirection == DOWN) && _player.velocity.x == 0)
					{
						_player.velocity.y = -_player.speed;
						break;
					}
					changeDirection();
					return;
				}
				case DOWN: 
				{
					if ((_lastDirection == NONE || _lastDirection == UP) && _player.velocity.x == 0)
					{
						
						_player.velocity.y = _player.speed;
						break;
					}
					changeDirection();
					return;
				}
				case LEFT: 
				{
					if ((_lastDirection == NONE || _lastDirection == RIGHT) && _player.velocity.y == 0)
					{
						_player.velocity.x = -_player.speed;
						break;
					}
					changeDirection();
					return;
				}
				case RIGHT: 
				{
					if ((_lastDirection == NONE || _lastDirection == LEFT) && _player.velocity.y == 0)
					{
						_player.velocity.x = _player.speed;
						break;
					}
					changeDirection();
					return;
				}
				default: 
				{
					break;
				}
			}
			_player.stopFollowingPath(true);
			_lastDirection = _nextDirection;
		}
		
		private static function changeDirection():void
		{
			var currentTile:FlxPoint = Maze.getTileByPoint(_player.getMidpoint());
			var nextTile:FlxPoint;
			switch (_nextDirection)
			{
				case UP: 
				{
					nextTile = Maze.getTileCenterByIndex(currentTile.x, currentTile.y - 1);
					break;
				}
				case DOWN: 
				{
					nextTile = Maze.getTileCenterByIndex(currentTile.x, currentTile.y + 1);
					break;
				}
				case LEFT: 
				{
					nextTile = Maze.getTileCenterByIndex(currentTile.x - 1, currentTile.y);
					break;
				}
				case RIGHT: 
				{
					nextTile = Maze.getTileCenterByIndex(currentTile.x + 1, currentTile.y);
					break;
				}
				default: 
				{
					break;
				}
			}
			var newPath:FlxPath = Maze.findPath(_player.getMidpoint(), nextTile);
			if (newPath != null)
			{
				_player.stopFollowingPath(true);
				_player.stopMoving();
				_player.followPath(newPath, _player.speed);
				_lastDirection = _nextDirection;
				_nextDirection = NONE;
			}
		}
	}
}